package com.example.reception.dao;

import com.example.reception.entity.Address;
import com.example.reception.entity.User;
import com.example.system.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author: adnaan
 * @create: 2023-10-22 22:08
 * @Description:
 */
@Component
public class UserDao {

    @Resource
    private MongoTemplate mongoTemplate;

    public ApiResponse findPaginated(Integer currentPage, Integer pageSize, String userName) {
        String reges = String.format("%s%s%s", "^.*", userName, ".*$");//匹配规则

        Pattern pattern = Pattern.compile(reges, Pattern.CASE_INSENSITIVE);//正则匹配规则--pattern的常量
        Query query = new Query(Criteria.where("username").regex(pattern));
        //查询集合记录总数
        long total = mongoTemplate.count(query, User.class);
        //分页查询
        List<User> comments = mongoTemplate.find(query.skip((currentPage - 1) * pageSize).limit(pageSize), User.class);
        //条件体
        Map<String, Object> res = new HashMap<>();
        res.put("total", total);
        res.put("data", comments);

        return ApiResponse.success(res);
    }

    public User get(Query query) {
        return mongoTemplate.findOne(query, User.class);
    }

    public List<Address> findAddress(Query addressQue) {
        return mongoTemplate.find(addressQue,Address.class);
    }

    public ApiResponse save(User s) {
        if(s.getUsername().isEmpty()||s.getPassword().isEmpty()){
            return ApiResponse.error("500", "用户名或密码不能为空");
        }
        Criteria criteriaName = Criteria.where("username").is(s.getUsername());
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaName);
        Query query = new Query(orCriteria);
        if (mongoTemplate.count(query, User.class) == 1) {
            return ApiResponse.error("500", "用户名重复");
        }
        return ApiResponse.success(mongoTemplate.save(s));
    }

    public User findByUsername(String username) {
        Criteria criteriaName = Criteria.where("username").is(username);
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaName);
        Query query = new Query(orCriteria);
        User user = get(query);
        return user;
    }

    public ApiResponse update(Query query, Update update) {
        mongoTemplate.updateMulti(query, update, User.class);
        return ApiResponse.success(update.getUpdateObject().get("$set"));
    }

    public ApiResponse updateAdr(Query query, Update update) {
        mongoTemplate.updateMulti(query, update, Address.class);
        return ApiResponse.success(update.getUpdateObject().get("$set"));
    }

    public ApiResponse saveAdr(Address address) {
        return ApiResponse.success(mongoTemplate.save(address));
    }

    public ApiResponse delAddress(Query query) {
        return ApiResponse.success(mongoTemplate.remove(query, Address.class));
    }

    public ApiResponse delUser(Query query) {
        return ApiResponse.success(mongoTemplate.remove(query, User.class));
    }
}
